//
//  setInfoViewController.m
//  theCoupleLive
//
//  Created by USER on 2016/12/3.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "setInfoViewController.h"
#import "coupleLiveAPI.h"
#import "loginViewController.h"
@interface setInfoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UIImage *headimg;
@end

@implementation setInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    [self.headImage addTarget:self action:@selector(chosePicture) forControlEvents:UIControlEventTouchUpInside];
//    NSLog(@"%@%@",self.teleNum,self.password);
    // Do any additional setup after loading the view from its nib.
}

-(void)chosePicture{
    UIActionSheet *actionSheet =
    [[UIActionSheet alloc] initWithTitle:nil
                                delegate:self
                       cancelButtonTitle:@"取消"
                  destructiveButtonTitle:@"拍照"
                       otherButtonTitles:@"我的相册", nil];
    actionSheet.tag = 201;
    [actionSheet showInView:self.view];
}

//实现actionsheet的方法
#pragma mark -UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag==201) {
        if (buttonIndex==0) {
            //链接相机
            if ([UIImagePickerController
                 isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerController *picker=[[UIImagePickerController alloc]init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.delegate=self;
            } else {
                NSLog(@"模拟器无法连接相机");
            }
        }
        else if(buttonIndex==1)
        {
            UIImagePickerController *picker=[[UIImagePickerController alloc]init];
            picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            picker.delegate=self;
            [self presentViewController:picker animated:YES completion:nil];
        }
        
    }
}

//头像
#pragma mark 选择头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"finish");
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    self.headimg = [info objectForKey:UIImagePickerControllerEditedImage];
    if (self.headimg == nil)
        self.headimg = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.headImage setBackgroundImage:self.headimg forState:UIControlStateNormal];
//    NSLog(@"%@",self.headimg);
}

#pragma mark 点击空白位置键盘隐藏
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.name resignFirstResponder];
    
    //textView键盘隐藏
    //[textView resignFirstResponder];
}
- (IBAction)btn_ok:(id)sender {
    if (self.headimg==nil) {
        [self alertfail:@"头像不能为空"];
    }
    else if ([self.name.text isEqualToString:@""]){
        [self alertfail:@"昵称不能为空"];
    }
    else{
        //image转NSString
        NSData *data=UIImageJPEGRepresentation(self.headimg, 1.0f);
        NSString *encodedImageStr=[data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        //注册
        [coupleLiveAPI registLiveWithName:self.teleNum pwd:self.password p_img:encodedImageStr p_birth:@"" p_address:@"" p_sex:@"" p_nickname:self.name.text success:^(id response) {
            if ([[response objectForKey:@"id"]isEqualToString:@"1"]) {
                [self alert:@"注册成功"];
            }
            else if ([[response objectForKey:@"id"]isEqualToString:@"3"]){
                [self alertfail:@"号码已注册"];
            }
            else{
                [self alertfail:@"注册失败"];
            }
        }];
    }
}

#pragma mark 返回
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark  成功alther弹窗跳转界面
-(void)alert:(NSString *)mes{
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示:" message:mes preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        loginViewController *login=[[loginViewController alloc]initWithNibName:@"loginViewController" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:login animated:YES];
    }];
    [controller addAction:action1];
    [self presentViewController:controller animated:YES completion:nil];
    
}

#pragma mark  失败alther弹窗
-(void)alertfail:(NSString *)mes{
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示:" message:mes preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [controller addAction:action1];
    [self presentViewController:controller animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
